package com.lzk.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.lzk.domain.ResponseResult;
import com.lzk.domain.dto.ExcelCategoryVo;
import com.lzk.domain.dto.GetCategoryMesVo;
import com.lzk.domain.entity.Category;
import com.lzk.domain.vo.CategoryVo;
import com.lzk.domain.vo.PageVo;
import com.lzk.enums.AppHttpCodeEnum;
import com.lzk.mapper.CategoryMapper;
import com.lzk.service.CategoryService;
import com.lzk.utils.BeanCopyUtils;
import com.lzk.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * author: 刘中魁
 */
@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<GetCategoryMesVo> getCategoryMesVos = categoryService.listAllCategory();

        return ResponseResult.okResult(getCategoryMesVos);
    }

    //-------------------获取所有 并分页---------------------
    @GetMapping("/list")
    public ResponseResult listAllCategoryByPageVo(Category category,Integer pageNum,Integer pageSize){
        PageVo pageVo = categoryService.listAllCategoryByPageVo(category,pageSize,pageNum);
        return ResponseResult.okResult(pageVo);
    }

    //---------------保存-----------------
    @PostMapping
    public ResponseResult addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseResult.okResult();
    }

    //----------------回显信息 通过id------------------
    @GetMapping("/{id}")
    public ResponseResult getCategoryMesById(@PathVariable Long id){
        Category category = categoryService.getCategoryMesById(id);
        return ResponseResult.okResult(category);
    }

    //------------------更新分类----------------
    @PutMapping
    public ResponseResult updateCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult delectCategory(@PathVariable Long id){
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }

    @GetMapping("/export")
    public void exportCategory(HttpServletResponse response){
        //设置下载文件的请求头
        try {
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> category = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(category, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            // 如果出现异常响应 json
//            response.reset(); //重置responset
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            WebUtils.renderString(response, JSON.toJSONString(result));
//            throw new RuntimeException(e);
        }


    }

}
